package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.Order;
import com.mofang.powerdekorhelper.model.OrderType;
import com.mofang.powerdekorhelper.model.ResultMessage;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.L;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.OrderManagerView;

import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/23 0023.
 */

public class OrderManagerPersenter extends BasePresent<OrderManagerView> {

    private final String TAG = this.getClass().getSimpleName();

    RetrofitSerives retrofitSerives;

    Call<Order> orderCall;

    Call<ResultMessage> closeCall;

    public void getOrderType() {
        OrderType orderType = new OrderType();

        List<OrderType.Data> datas = new ArrayList<>();

        OrderType.Data data = new OrderType().new Data();
        data.setOrderId(0);
        data.setOrderName("全部");
        datas.add(data);

        data = new OrderType().new Data();
        data.setOrderId(1);
        data.setOrderName("物流");
        datas.add(data);

        data = new OrderType().new Data();
        data.setOrderId(2);
        data.setOrderName("安装");
        datas.add(data);

        data = new OrderType().new Data();
        data.setOrderId(3);
        data.setOrderName("售后");
        datas.add(data);

        data = new OrderType().new Data();
        data.setOrderId(4);
        data.setOrderName("关闭");
        datas.add(data);

        orderType.setData(datas);

        view.setOrderType(orderType);
    }

    public void getOrderManagerList(final String requestStr) {

        L.i(TAG, "requestStr---------------->" + requestStr);

        view.showProgress();
        final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        orderCall = retrofitSerives.getOrder(body);
        orderCall.enqueue(new Callback<Order>() {
            @Override
            public void onResponse(Call<Order> call, Response<Order> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0") && response.body().getResult() != null) {
                        if (response.body().getResult().getData().size() > 0) {
                            view.setOrderList(response.body());
                        } else {
                            view.setOrderList(response.body());
                            view.showNullLayout();
                        }
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<Order> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    public void getCloseOrder(final String requestStr) {
        view.showProgress();
        final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        closeCall = retrofitSerives.getCloseOrder(body);
        closeCall.enqueue(new Callback<ResultMessage>() {
            @Override
            public void onResponse(Call<ResultMessage> call, Response<ResultMessage> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {
                        view.setCloseOrder(response.body());
                    } else {
                        view.onError(Constants.COMMIT_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.COMMIT_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage> call, Throwable t) {
                view.onError(Constants.COMMIT_ERROR_INFO);
            }
        });
    }

    public void getRefreshOrder(String requestStr) {

        view.showProgress();
        final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        closeCall = retrofitSerives.getRefreshOrder(body);

        closeCall.enqueue(new Callback<ResultMessage>() {
            @Override
            public void onResponse(Call<ResultMessage> call, Response<ResultMessage> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        view.setRefreshOrder(response.body());
                    } else {
                        view.onError(Constants.COMMIT_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.COMMIT_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage> call, Throwable t) {
                view.onError(Constants.COMMIT_ERROR_INFO);
            }
        });
    }

    @Override
    public void stopRequest() {
        if (orderCall != null) {
            orderCall.cancel();
        }
    }
}
